package nl.hu.vakantievibes.ui;

import nl.hu.vakantievibes.client.FieldVerifier;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Registreer {
	
	private Label lblName, lblPass1, lblPass2;
	private TextBox txtName;
	private PasswordTextBox txtPass1, txtPass2;
	private Button btnSubmit, btnCancel;
	private VerticalPanel vp; 
	private HorizontalPanel buttonPanel;
	
	public Registreer() {	
		
		lblName = new Label("Gebruikersnaam:");
		txtName = new TextBox();
		lblPass1 = new Label("Wachtwoord:");
		txtPass1 = new PasswordTextBox();
		lblPass2 = new Label("Wachtwoord: (check)");
		txtPass2 = new PasswordTextBox();
		btnSubmit = new Button("Registreer");
		btnCancel = new Button("Reset");
		
		vp = new VerticalPanel();
		buttonPanel = new HorizontalPanel();
		
		KeyPressHandler loginKeyHandler = new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
					handleFields();
				}			
			}
		};
			
		txtName.addKeyPressHandler(loginKeyHandler);
		txtPass1.addKeyPressHandler(loginKeyHandler);
		txtPass2.addKeyPressHandler(loginKeyHandler);
		
		btnSubmit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				handleFields();
			}
		});
		
		btnCancel.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event){
				resetFields();
			}
		});
			
	}
	
	public Panel createPage() {
		vp.add(lblName); vp.add(txtName);
		vp.add(lblPass1); vp.add(txtPass1);
		vp.add(lblPass2); vp.add(txtPass2);
		buttonPanel.add(btnSubmit); buttonPanel.add(btnCancel);
		vp.add(buttonPanel); 
		return vp;
	}

	private void handleFields() {
		int b = 0;
		if (txtName.getText().matches("^[A-Z-a-z0-9]+$") &&
			FieldVerifier.get().checkField(txtName.getText(), 4, 30)) {
			txtName.setStyleName("green-border");
			b += 1;
		} else {
			txtName.setStyleName("red-border");
		}
		
		if (FieldVerifier.get().checkField(txtPass1.getText(), 4, 30) &&
				txtPass1.getText().equals(txtPass2.getText())) {
			txtPass1.setStyleName("green-border");
			txtPass2.setStyleName("green-border");
			b += 1;
		} else {
			txtPass1.setStyleName("red-border");
			txtPass2.setStyleName("red-border");
		}
		
		if(b == 2) {
			FieldVerifier.get().checkRegister(txtName.getText(), txtPass1.getText());
			/*if() {
				DeckPanel pages = PageManager.getPageManager().getPages();
				pages.remove(1);
				pages.insert(new Registreer().createPage(), 1);
				pages.showWidget(0);
				History.newItem("login");
			}*/
		}
	}
	
	private void resetFields() {
		txtName.setText(""); txtName.setStyleName("");
		txtPass1.setText(""); txtPass1.setStyleName("");
		txtPass2.setText(""); txtPass2.setStyleName("");
	}
	
}
